package utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import modele.*;

import net.sf.ezmorph.object.DateMorpher;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONUtils;

import org.springframework.stereotype.Component;



/**
 * Util class. Contains some common methods that can be used
 * for any class
 * 
 * @author Loiane Groner
 * http://loianegroner.com (English)
 * http://loiane.com (Portuguese)
 */
@Component
public class Util {
	
	/**
	 * Get list of Contacts from request.
	 * @param data - json data from request 
	 * @return list of Contacts
	 */
	/**
	 * Get list of Contacts from request.
	 * @param data - json data from request 
	 * @return list of Contacts
	 */
	
	@SuppressWarnings("unchecked")
	public static List<Integer> getListIdFromJSON(Object data){
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Integer> idContacts = (List<Integer>) JSONArray.toCollection(jsonArray,Integer.class);
		return idContacts;
	}

	public static List<Famille> getFamilleFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Famille> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListFamillesFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Famille contact = getFamilleFromJSON(data);
			list = new ArrayList<Famille>();
			list.add(contact);
		}
		
		return list;
	}

	public static Famille getFamilleFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONObject jsonObject = JSONObject.fromObject(data);
		Famille newFamille = (Famille) JSONObject.toBean(jsonObject, Famille.class);
		return newFamille;
	}

	public static List<Famille> getListFamillesFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Famille> newFamilles = (List<Famille>) JSONArray.toCollection(jsonArray,Famille.class);
		return newFamilles;
	}

	public static List<Depot> getDepotFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Depot> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListDepotsFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Depot depot = getDepotFromJSON(data);
			list = new ArrayList<Depot>();
			list.add(depot);
		}
		return list;
	}

	public static Depot getDepotFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Depot newDepot = (Depot) JSONObject.toBean(jsonObject, Depot.class);
		return newDepot;
	}

	public static List<Depot> getListDepotsFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Depot> newDepots = (List<Depot>) JSONArray.toCollection(jsonArray,Depot.class);
		return newDepots;
	}

	public static List<Depotcontact> getDepotcontactFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Depotcontact> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListDepotcontactsFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Depotcontact depot = getDepotcontactFromJSON(data);
			list = new ArrayList<Depotcontact>();
			list.add(depot);
		}
		return list;
	}
	public static Depotcontact getDepotcontactFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Depotcontact newDepotcontact = (Depotcontact) JSONObject.toBean(jsonObject, Depotcontact.class);
		return newDepotcontact;
	}

	public static List<Depotcontact> getListDepotcontactsFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Depotcontact> newDepotcontacts = (List<Depotcontact>) JSONArray.toCollection(jsonArray,Depotcontact.class);
		return newDepotcontacts;
	}

	public static List<Article> getArticleFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Article> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListArticlesFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Article article = getArticleFromJSON(data);
			list = new ArrayList<Article>();
			list.add(article);
		}
		return list;
	}
	public static Article getArticleFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Article newArticle = (Article) JSONObject.toBean(jsonObject, Article.class);
		return newArticle;
	}

	public static List<Article> getListArticlesFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Article> newArticles = (List<Article>) JSONArray.toCollection(jsonArray,Article.class);
		return newArticles;
	}

	public static List<Articlestock> getArticlestockFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Articlestock> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListArticlestocksFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Articlestock articlestock = getArticlestockFromJSON(data);
			list = new ArrayList<Articlestock>();
			list.add(articlestock);
		}
		return list;
	}
	public static Articlestock getArticlestockFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Articlestock newArticlestock = (Articlestock) JSONObject.toBean(jsonObject, Articlestock.class);
		return newArticlestock;
	}

	public static List<Articlestock> getListArticlestocksFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Articlestock> newArticlestocks = (List<Articlestock>) JSONArray.toCollection(jsonArray,Articlestock.class);
		return newArticlestocks;
	}

	public static List<Utilisateur> getUtilisateurFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Utilisateur> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListUtilisateursFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Utilisateur utilisateur = getUtilisateurFromJSON(data);
			list = new ArrayList<Utilisateur>();
			list.add(utilisateur);
		}
		return list;
	}
	public static Utilisateur getUtilisateurFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Utilisateur newUtilisateur = (Utilisateur) JSONObject.toBean(jsonObject, Utilisateur.class);
		return newUtilisateur;
	}

	public static List<Utilisateur> getListUtilisateursFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Utilisateur> newUtilisateurs = (List<Utilisateur>) JSONArray.toCollection(jsonArray,Utilisateur.class);
		return newUtilisateurs;
	}

	public static List<Profil> getProfilFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Profil> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListProfilsFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Profil profil = getProfilFromJSON(data);
			list = new ArrayList<Profil>();
			list.add(profil);
		}
		return list;
	}
	public static Profil getProfilFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Profil newProfil = (Profil) JSONObject.toBean(jsonObject, Profil.class);
		return newProfil;
	}

	public static List<Profil> getListProfilsFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Profil> newProfils = (List<Profil>) JSONArray.toCollection(jsonArray,Profil.class);
		return newProfils;
	}

	
	public static  List<Moduleprofil> ModuleprofilDaoToModuleprofil(List<Moduleprofil> l)
	{
		List<Moduleprofil> list = new ArrayList<Moduleprofil>();
		if(l.size()>0)
		{
			for(Moduleprofil item:l)
			{
				Moduleprofil mpf = new Moduleprofil();
				//item.getIdModuleprofil(),item.getR(),item.getC(),item.getU(),item.getD(),item.getModule().getIdModule(),item.getProfil().getIdProfil()
				list.add(mpf);
			}
		}
		return list;
	}
	
	public static List<Moduleprofil> getModuleprofilFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Moduleprofil> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListModuleprofilsFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Moduleprofil moduleprofil = getModuleprofilFromJSON(data);
			list = new ArrayList<Moduleprofil>();
			list.add(moduleprofil);
		}
		return list;
	}
	
	public static Moduleprofil getModuleprofilFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Moduleprofil newModuleprofil = (Moduleprofil) JSONObject.toBean(jsonObject, Moduleprofil.class);
		return newModuleprofil;
	}

	public static List<Moduleprofil> getListModuleprofilsFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Moduleprofil> newModuleprofils = (List<Moduleprofil>) JSONArray.toCollection(jsonArray,Moduleprofil.class);
		
		return newModuleprofils;
	}	
	
	public static String getEncodedPassword(String key) {
		 byte[] uniqueKey = key.getBytes();
		 byte[] hash = null;
		 try {
		 hash = MessageDigest.getInstance("MD5").digest(uniqueKey);
		 } catch (NoSuchAlgorithmException e) {
		 throw new Error("no MD5 support in this VM");
		 }
		 StringBuffer hashString = new StringBuffer();
		 for ( int i = 0; i < hash.length; ++i ) {
		 String hex = Integer.toHexString(hash[i]);
		 if ( hex.length() == 1 ) {
		 hashString.append('0');
		 hashString.append(hex.charAt(hex.length()-1));
		 } else {
		 hashString.append(hex.substring(hex.length()-2));
		 }
		 }
		 return hashString.toString();
	}

	public static List<Unitevente> getUniteventeFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Unitevente> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListUniteventesFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Unitevente profil = getUniteventeFromJSON(data);
			list = new ArrayList<Unitevente>();
			list.add(profil);
		}
		return list;
	}
	public static Unitevente getUniteventeFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Unitevente newUnitevente = (Unitevente) JSONObject.toBean(jsonObject, Unitevente.class);
		return newUnitevente;
	}

	public static List<Unitevente> getListUniteventesFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Unitevente> newUniteventes = (List<Unitevente>) JSONArray.toCollection(jsonArray,Unitevente.class);
		return newUniteventes;
	}

	public static List<Fournisseur> getFournisseurFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Fournisseur> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListFournisseursFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Fournisseur f = getFournisseurFromJSON(data);
			list = new ArrayList<Fournisseur>();
			list.add(f);
		}
		return list;
	}
	public static Fournisseur getFournisseurFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Fournisseur newFournisseur = (Fournisseur) JSONObject.toBean(jsonObject, Fournisseur.class);
		return newFournisseur;
	}

	public static List<Fournisseur> getListFournisseursFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Fournisseur> newFournisseurs = (List<Fournisseur>) JSONArray.toCollection(jsonArray,Fournisseur.class);
		return newFournisseurs;
	}

	public static List<Client> getClientFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Client> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListClientsFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Client c = getClientFromJSON(data);
			list = new ArrayList<Client>();
			list.add(c);
		}
		return list;
	}
	public static Client getClientFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Client newClient = (Client) JSONObject.toBean(jsonObject, Client.class);
		return newClient;
	}

	public static List<Client> getListClientsFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Client> newClients = (List<Client>) JSONArray.toCollection(jsonArray,Client.class);
		return newClients;
	}

	public static List<Docligne> getDocligneFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Docligne> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListDoclignesFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Docligne c = getDocligneFromJSON(data);
			list = new ArrayList<Docligne>();
			list.add(c);
		}
		return list;
	}
	public static Docligne getDocligneFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Docligne newDocligne = (Docligne) JSONObject.toBean(jsonObject, Docligne.class);
		return newDocligne;
	}

	public static List<Docligne> getListDoclignesFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Docligne> newDoclignes = (List<Docligne>) JSONArray.toCollection(jsonArray,Docligne.class);
		return newDoclignes;
	}

	public static List<Docentete> getDocenteteFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Docentete> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListDocentetesFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Docentete c = getDocenteteFromJSON(data);
			list = new ArrayList<Docentete>();
			list.add(c);
		}
		return list;
	}
	public static Docentete getDocenteteFromJSON(Object data) {
		JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[] {"MM-dd-yyyy"}) ); 
		JSONObject jsonObject = JSONObject.fromObject(data);
		Docentete newDocentete = (Docentete) JSONObject.toBean(jsonObject, Docentete.class);
		return newDocentete;
	}

	public static List<Docentete> getListDocentetesFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Docentete> newDocentetes = (List<Docentete>) JSONArray.toCollection(jsonArray,Docentete.class);
		return newDocentetes;
	}

	public static List<Taxe> getTaxeFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Taxe> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListTaxesFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Taxe c = getTaxeFromJSON(data);
			list = new ArrayList<Taxe>();
			list.add(c);
		}
		return list;
	}
	public static Taxe getTaxeFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Taxe newTaxe = (Taxe) JSONObject.toBean(jsonObject, Taxe.class);
		return newTaxe;
	}

	public static List<Taxe> getListTaxesFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Taxe> newTaxes = (List<Taxe>) JSONArray.toCollection(jsonArray,Taxe.class);
		return newTaxes;
	}

	public static List<Compteg> getComptegFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Compteg> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListComptegsFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Compteg c = getComptegFromJSON(data);
			list = new ArrayList<Compteg>();
			list.add(c);
		}
		return list;
	}
	public static Compteg getComptegFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Compteg newCompteg = (Compteg) JSONObject.toBean(jsonObject, Compteg.class);
		return newCompteg;
	}

	public static List<Compteg> getListComptegsFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Compteg> newComptegs = (List<Compteg>) JSONArray.toCollection(jsonArray,Compteg.class);
		return newComptegs;
	}

	public static List<Catcompta> getCatcomptaFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Catcompta> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListCatcomptasFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Catcompta c = getCatcomptaFromJSON(data);
			list = new ArrayList<Catcompta>();
			list.add(c);
		}
		return list;
	}
	public static Catcompta getCatcomptaFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Catcompta newCatcompta = (Catcompta) JSONObject.toBean(jsonObject, Catcompta.class);
		return newCatcompta;
	}

	public static List<Catcompta> getListCatcomptasFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Catcompta> newCatcomptas = (List<Catcompta>) JSONArray.toCollection(jsonArray,Catcompta.class);
		return newCatcomptas;
	}

	public static List<Articletaxe> getArticletaxeFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Articletaxe> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListArticletaxesFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Articletaxe c = getArticletaxeFromJSON(data);
			list = new ArrayList<Articletaxe>();
			list.add(c);
		}
		return list;
	}
	public static Articletaxe getArticletaxeFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Articletaxe newArticletaxe = (Articletaxe) JSONObject.toBean(jsonObject, Articletaxe.class);
		return newArticletaxe;
	}

	public static List<Articletaxe> getListArticletaxesFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Articletaxe> newArticletaxes = (List<Articletaxe>) JSONArray.toCollection(jsonArray,Articletaxe.class);
		return newArticletaxes;
	}

	public static List<Familletaxe> getFamilletaxeFromRequest(Object data) {
		// TODO Auto-generated method stub
		List<Familletaxe> list;
		//it is an array - have to cast to array object
		if (data.toString().indexOf('[') > -1){
			list = getListFamilletaxesFromJSON(data);
		} else { //it is only one object - cast to object/bean
			Familletaxe c = getFamilletaxeFromJSON(data);
			list = new ArrayList<Familletaxe>();
			list.add(c);
		}
		return list;
	}
	public static Familletaxe getFamilletaxeFromJSON(Object data) {
		JSONObject jsonObject = JSONObject.fromObject(data);
		Familletaxe newFamilletaxe = (Familletaxe) JSONObject.toBean(jsonObject, Familletaxe.class);
		return newFamilletaxe;
	}

	public static List<Familletaxe> getListFamilletaxesFromJSON(Object data) {
		// TODO Auto-generated method stub
		JSONArray jsonArray = JSONArray.fromObject(data);
		List<Familletaxe> newFamilletaxes = (List<Familletaxe>) JSONArray.toCollection(jsonArray,Familletaxe.class);
		return newFamilletaxes;
	}
	
}
